package com.mvc.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.mvc.web.common.utils.OSSConfigure;

public class FileOpration {

	
	public  static void main(String[] args){
		long time1=System.currentTimeMillis();
		String path = "D:\\git_work_space\\images";
		File[] pics = new File(path).listFiles();
		if (pics != null && pics.length > 0) {
			for (File pic : pics) {
				String allname = pic.getName();
				try {
					if(pic.length()>0){
						System.out.println(uploadFile(pic,OSSConfigure.getBucketName(),allname));
					}
					pic=null;
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		long time2 = System.currentTimeMillis();
		System.out.println("send success! 用时："+(time2-time1));
	}
	
	public static String uploadFile(File file,String bucketName,String file_name) throws Exception{  
    	InputStream fileContent=null;  
    	fileContent=new FileInputStream(file);  
    	String  fileName= file.getName();
    	OSSClient ossClient=new OSSClient(OSSConfigure.getEndpoint(), OSSConfigure.getAccessKeyId(), OSSConfigure.getAccessKeySecret());  
    	
    	//创建上传Object的Metadata  
    	ObjectMetadata objectMetadata=new ObjectMetadata();  
    	objectMetadata.setContentLength(fileContent.available());  
    	objectMetadata.setCacheControl("no-cache");  
    	objectMetadata.setHeader("Pragma", "no-cache");  
//    	objectMetadata.setContentType(OSSManageUtil.contentType(fileName.substring(fileName.lastIndexOf(".")+1)));  
    	//attachment，inline
    	objectMetadata.setContentType("application/vnd.android.package-archive");
    	objectMetadata.setContentDisposition("attachment;filename=" + file_name);  
    	//上传文件  
    	ossClient.putObject(bucketName,   fileName, fileContent, objectMetadata);  
    	System.out.println(file.getName());  
    	fileContent=null;
    	objectMetadata=null;
    	ossClient=null;
    	file=null;
    	return OSSConfigure.getAccessUrl() + fileName;  
    }
}
